package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcFollowDataHeartMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcFollowDataHeart;
import com.csun.cmny.provider.model.dto.follow.FollowDataPageQuery;
import com.csun.cmny.provider.model.dto.follow.UpFollowDataHeartDto;
import com.csun.cmny.provider.model.enums.follow.FollowHeartStatustEnum;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcFollowDataHeartService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-16 21:47
 * @Description: the class dmc follow data heart service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcFollowDataHeartServiceImpl extends BaseService<DmcFollowDataHeart>
        implements DmcFollowDataHeartService {

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private DmcFollowDataHeartMapper followDataHeartDao;

    /**
     * query dmc follow data heart list page
     *
     * @param followDataPageQuery follow data query page
     *
     * @return the page info
     */
    @Override
    public PageInfo queryDmcFollowDataHeartListWithPage(FollowDataPageQuery followDataPageQuery) {

        PageHelper.startPage(followDataPageQuery.getPageNum(), followDataPageQuery.getPageSize());

        List<DmcFollowDataHeart> dmcFollowDataHeartList = followDataHeartDao
                .queryDmcFollowDataHeartListWithPage(followDataPageQuery);

        return new PageInfo<>(dmcFollowDataHeartList);
    }

    /**
     * up follow data heart
     *
     * @param upFollowDataHeartDto up follow data heart dto
     * @param loginAuthDto         login auth dto
     * @return the int.
     */
    @Override
    public Integer upFollowDataHeart(UpFollowDataHeartDto upFollowDataHeartDto, LoginAuthDto loginAuthDto) {

        // 1.校验设备
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsFollowByDeviceId(upFollowDataHeartDto.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610041, upFollowDataHeartDto.getDeviceId());
        }

        // 2.校验参数
        Preconditions.checkArgument(!PubUtils.isNull(upFollowDataHeartDto.getHeartRate()),
                cmnyMessageSource.getMessage("follow.up.heart.rate.null", null));

        // 3.换算状态
        String status = FollowHeartStatustEnum.getStatus(upFollowDataHeartDto.getHeartRate());
        if (PubUtils.isNull(status)) {
            status = cmnyMessageSource.getMessage("follow.heart.rate.status.error", null);
        }

        // 4.插入数据
        DmcFollowDataHeart dmcFollowDataHeart = new DmcFollowDataHeart();
        BeanUtils.copyProperties(upFollowDataHeartDto, dmcFollowDataHeart);
        dmcFollowDataHeart.setStatus(status);
        dmcFollowDataHeart.setUpdateInfo(loginAuthDto);

        return followDataHeartDao.insertSelective(dmcFollowDataHeart);
    }

    @Override
    public Map<String, Object> getRecentData(Integer oldmanId) {
        return followDataHeartDao.getRecentData(oldmanId);
    }

    @Override
    public List<Map<String, Object>> getAllOldmanData(Integer oldmanId) {
        return followDataHeartDao.getAllOldmanData(oldmanId);
    }

}
